package com.mall.order.web;

import com.mall.common.page.PageData;
import com.mall.order.dto.OrderItemDTO;
import com.mall.order.service.OrderItemService;
import com.mall.order.service.OrderService;
import com.mall.order.vo.OrderConformVo;
import com.mall.order.vo.OrderResponseVo;
import com.mall.order.vo.OrderSubmitVo;
import com.mall.order.vo.OrderVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: 谢君臣
 * @Date: 2022/8/9-20:31
 * @version: 1.0
 * @function:
 */
@Controller
public class OrderIndexController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;

    @GetMapping("/order/list")
    public String pageList(@ApiIgnore @RequestParam Map<String, Object> params,Model model){
        PageData<OrderVo> pageData = orderService.getPage(params);
        model.addAttribute("pageData",pageData);
        return "list";
    }



    @GetMapping("/order/toTrade")
    public String toTrade(Model model){
        OrderConformVo orderConformVo = orderService.orderConform();
        model.addAttribute("conformVo",orderConformVo);
        return "conform";
    }

    @PostMapping("/order/submit")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes){
        OrderResponseVo responseVo = null;
        try {
            responseVo = orderService.submitOrder(orderSubmitVo);
            if (responseVo.getCode()==0){
                model.addAttribute("order",responseVo);
                return "pay";
            }else
                throw new RuntimeException("异常");
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("msg","订单提交异常");
            return "redirect:http://order.mall.com/order/toTrade";
        }
    }

}
